package CutLoad.mc
{
	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * 可以控制一组元件实例，一次显示一个 
	 * @author hai
	 * 
	 */	
	public class OnlyShowOne
	{
		private var arr:Array;
		private var currentIndex:Sprite;
		private var nextShow:Sprite;
		private var current:Number;
		//
		private var isShow_Transition:Boolean=false;
		private var data_Transition:Object;
		private var Transition_enterFrame:Sprite;
		private var show_Transition_alpha_speed:Number;
		//
		private var funChang:Function;
		public function OnlyShowOne(arr:Array,showIndex:Number=0)
		{
			Transition_enterFrame=new Sprite();
			this.arr=arr;
			for(var i:Number=0;i<arr.length;i++){
				var sp:Sprite=arr[i] as Sprite;
				if(i==showIndex){
					sp.visible=true;
					currentIndex=sp;
				}else{
					sp.visible=false;
				}
			}
		}
		/**
		 * 显示某一个 
		 * @param index 显示哪一个下标从0开始
		 * @param funChang 改变的时候，回调的方法  格式;function(当前元件，下一个元件);
		 * @return 
		 * 
		 */		
		public function show(index:uint,funChang:Function=null):Boolean{
			if(current==index) return false;
			if(isShow_Transition) return false;
			if(index<0||index>arr.length-1) return false;
			//设置改变回调方法
			if(funChang==null){
				funChang=function(currentsp:Sprite,nextsp:Sprite):void{};
			}
			this.funChang=funChang;
			var sp:Sprite=arr[index] as Sprite;
			funChang(currentIndex,sp)
			currentIndex.visible=false;
			currentIndex=sp;
			sp.visible=true;
			current=index;
			return true;
		}
		/**
		 * 显示某一个，加有淡入淡出效果 
		 * @param index
		 * @param funChang
		 * 
		 * @return 
		 * 
		 */		
		public function show_Transition_alpha(index:uint,speed:Number=0.1,funChang:Function=null):Boolean{
			if(current==index) return false;
			if(isShow_Transition) return false
			if(index<0||index>arr.length-1) return false;
			//设置改变回调方法
			if(funChang==null){
				funChang=function(currentsp:Sprite,nextsp:Sprite):void{};
			}
			this.funChang=funChang;
			this.show_Transition_alpha_speed=speed;
			//
			isShow_Transition=true;
			if(Transition_enterFrame!=null){
				if(Transition_enterFrame.hasEventListener(Event.ENTER_FRAME)){
					Transition_enterFrame.removeEventListener(Event.ENTER_FRAME,enter_frame);
				}
			}
			current=index;
			var sp:Sprite=arr[index] as Sprite;
			funChang(currentIndex,sp)
			nextShow=sp;
			nextShow.visible=true;
			nextShow.alpha=0;
			currentIndex.visible=true;
			currentIndex.alpha=1;
			Transition_enterFrame.addEventListener(Event.ENTER_FRAME,enter_frame);
			return true;
		}
		private function enter_frame(e:Event):void{
			//trace(currentIndex.alpha)
			currentIndex.alpha-=show_Transition_alpha_speed;
			nextShow.alpha+=show_Transition_alpha_speed;
			if(currentIndex.alpha<0){
				currentIndex.alpha=1;
				nextShow.alpha=1;
				currentIndex.visible=false;
				currentIndex=nextShow;
				currentIndex.visible=true;
				//
				isShow_Transition=false;
				Transition_enterFrame.removeEventListener(Event.ENTER_FRAME,enter_frame);
			}
		}
		
	}
}